package src.leetcode.greedy.medium_0376;

public class wiggleMaxLength {
    public static void main(String[] args) {
        Solution solution = new Solution();
        System.out.println(solution.wiggleMaxLength(new int[]{1,1,1, 7, 4,4,4,4, 9, 2, 5}));
    }
}

class Solution {
    public int wiggleMaxLength(int[] nums) {
        if(nums.length < 2) return nums.length;

        int count = 1;
        int flag = 0;

        for (int i = 1; i < nums.length; i++) {
            if(flag == 0){
                if(nums[i] != nums[i-1]) {
                    if(nums[i] > nums[i-1]) flag = 1;
                    else flag = -1;
                    count++;
                }
            }else if((nums[i] - nums[i-1]) * flag < 0) {
                count++;
                flag = -flag;
            }
        }
        return count;
    }
}

